package com.web.controoller;

import com.web.constant.UserConstants;
import com.web.pojo.domain.Address;
import com.web.pojo.domain.UserInfo;
import com.web.pojo.vo.AuthorizeDTO;
import com.web.pojo.vo.CommonResult;
import com.web.service.AddressServer;
import com.web.service.UserServer;
import com.web.utils.SecurityUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: wws
 * @Date: 2021/1/12 15:55
 * describe:
 */
@CrossOrigin
@RestController
@RequestMapping("/address")
public class AddressTreeController {

    @Autowired
    AddressServer addressTreeServer;

    @Autowired
    UserServer userServer;


    @PostMapping("/insert")
    @ApiOperation("添加设备树节点信息")
    public CommonResult insertAddress(@RequestBody Address addressTree) {
        if (addressTree.getAddressName() == null) {
            return CommonResult.fail("添加失败，请填写地址名称");
        }
        if (addressTree.getPid() == null) {
            addressTree.setPid(0);
        }
        if (!addressTreeServer.checkAddressNameUnique(addressTree)) {
            return CommonResult.fail("添加失败," + addressTree.getAddressName() + "已存在");
        }
        addressTree.setCreateBy(SecurityUtils.getUsername());
//        addressTree.setCreateBy("wws");
        return CommonResult.insert(addressTreeServer.insertAddress(addressTree));
    }

    @PostMapping("/update")
    @ApiOperation("修改设备树节点信息")
    public CommonResult updateAddress(@RequestBody Address addressTree) {
        if (UserConstants.NOT_UNIQUE.equals(addressTreeServer.checkAddressNameUnique(addressTree))) {
            return CommonResult.fail("修改失败," + addressTree.getAddressName() + "已存在");
        } else if (addressTree.getPid().equals(addressTree.getAddressId())) {
            return CommonResult.fail("修改失败,上级地址不能是自己");
        }
        addressTree.setCreateBy(SecurityUtils.getUsername());
//        addressTree.setCreateBy("wws");
        return CommonResult.update(addressTreeServer.updateAddress(addressTree));
    }

    @PostMapping("/delete")
    @ApiOperation("删除设备树节点")
    public CommonResult deleteAddress(@RequestBody Integer addressId) {
//        if (addressTreeServer.checkAddressExistDevice(addressId)) {
//            return CommonResult.fail("该地址节点下存在设备，不允许删除");
//        }
//        if (addressTreeServer.checkAddressExistGateWay(addressId)) {
//            return CommonResult.fail("该地址节点下存在网关，不允许删除");
//        }
//        if (addressTreeServer.checkAddressExistChild(addressId)){
//            return CommonResult.fail("该地址节点下存在字地址，不允许删除");
//        }
        String updateBy = SecurityUtils.getUsername();
        return CommonResult.delete(addressTreeServer.deleteAddress(updateBy, addressId));
    }

    @PostMapping("/checkAddressNameUnique")
    @ApiOperation("效验地址树节点名称")
    public CommonResult checkAddressNameUnique(@RequestBody Address addressTree) {
        if (addressTreeServer.checkAddressNameUnique(addressTree)) {
            return CommonResult.success("该名称可用");
        } else {
            return CommonResult.fail("该名称已存在");
        }
    }

    @PostMapping("/selectList")
    @ApiOperation("查看地址树列表")
    public CommonResult selectAddressList(@RequestBody(required = false) String addressName) {
        Integer userId =SecurityUtils.getUserId();
        List<Address> list = addressTreeServer.selectAddressList(userId,addressName);
        return CommonResult.select(list);
    }

    @PostMapping("/selectChildNodeList")
    @ApiOperation("查看地址子节点列表")
    public CommonResult selectAddressChildNodeList(@RequestBody Integer addressId) {
        List<Address> list = addressTreeServer.selectAddressChildNodeList(addressId);
        return CommonResult.select(list);
    }

    @PostMapping("/details")
    @ApiOperation("查看地址树详情")
    public CommonResult selectAddressDetails(@RequestBody Integer addressId) {
        Address info = addressTreeServer.selectAddress(addressId);
        return CommonResult.select(info);
    }

    @PostMapping("/selectAddressRootNode")
    @ApiOperation("查看地址根节点")
    public CommonResult selectAddressRootNode(){
        Integer userId = SecurityUtils.getUserId();
        return CommonResult.select(addressTreeServer.selectAddressRootNode(userId));
    }

    @PostMapping("/authorize")
    @ApiOperation("授权地址树")
    public CommonResult authorizeAddress(@RequestBody AuthorizeDTO dto){
        UserInfo userInfo = userServer.selectUserInfoByUsername(dto.getUsername());
        System.out.println(userInfo);
        if (userInfo == null) {
            return CommonResult.error("用户不存在");
        }
        addressTreeServer.authorizeAddress(dto.getIds(),userInfo.getUserId());
        return CommonResult.success("授权成功");
    }
}
